<template>
  <div class="mt-1">
    <van-form>
      <h5 class="titlestyle">
        <span style="margin: 0 0 0 0.3rem"
          >当前产区:{{ this.$store.getters.productionLine }}
        </span>
        <!-- <span style="margin: 0 0 0 1rem">
          当前生产班次:{{ this.backData.WorkTeam
          }}{{ this.backData.WorkShift }}</span
        >
        <span style="margin: 0 0 0 3rem">段:{{ this.backData.WorkNo }}</span> -->
      </h5>
      <!-- <van-field
        readonly
        clickable
        :value="productorder"
        label="物料SKU"
        placeholder="点击选择"
        @click="SKUPicker = true"
      />
      <van-popup v-model="SKUPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="columns"
          @confirm="SKUAdjustConfirm"
          @cancel="SKUPicker = false"
        />
      </van-popup> -->
      <van-field
        v-model="code"
        clearable
        ref="pmfocus"
        @focus="stopKeyborad"
        label="返工桶ID"
        placeholder="请输入返工桶id"
      />
      <!-- 弹出框 ,初改-->
      <van-dialog
        v-model="visible"
        show-cancel-button
        :beforeClose="handleSure"
        confirm-button-color="rgba(25, 137, 250,1)"
      >
        <div class="ip-container">
          <van-field label="物料号" :value="this.backData.ProductId" readonly />
          <van-field
            label="物料名称"
            :value="this.backData.ProductName"
            readonly
          />
          <van-field
            label="玛氏批次"
            :value="this.backData.AtlasBatch"
            readonly
          />
          <van-field
            label="入库时间"
            :value="this.backData.WorkDate"
            readonly
          />
          <van-field label="收货数量" required v-model="newNum" />
        </div>
      </van-dialog>
      <ag-grid-vue
        style="width: 100%; height: 55vh"
        class="ag-theme-alpine mt-1"
        :columnDefs="columnDefs"
        :defaultColDef="defaultColDef"
        :gridOptions="gridOptions"
        :singleClickEdit="true"
        rowSelection="multiple"
        :rowData="rowData"
      >
      </ag-grid-vue>
      <van-button block type="info" class="mt-1" @click="handleSubmit"
        >提交</van-button
      >
    </van-form>
  </div>
</template>

<script>
import { Dialog } from "vant";
import { Picker } from "vant";
import { CODE_LENGTH } from "../../config";
import { parseQRCode, toast } from "../../utils/common";
import { AgGridVue } from "ag-grid-vue";
import { AgGrid } from "../../mixins/AgGrid";
import {
  //getSkuDeatil,
  getbucketDeatil,
  SubmitInStorage,
  //getReWorkSearchList,
} from "../../api/return";

export default {
  name: "ReturnAccept",
  components: {
    AgGridVue,
    [Dialog.Component.name]: Dialog.Component,
    [Picker.name]: Picker,
  },
  mixins: [AgGrid],
  data() {
    return {
      code: "",
      Code: "",
      visible: false,
      //SKUPicker: false, //选择框可见
      savenum: "",
      chooseCode: [],
      newNum: "",
      OldNum: "",
      productorder: "",
      columnDefs: [
        { headerName: "物料号", field: "ProductId", width: 90 },
        { headerName: "物料名称", field: "ProductName", width: 125 },
        { headerName: "入库日期", field: "ProductionDate", width: 100 },
        { headerName: "数量", field: "Num", width: 70 },
      ],
      rowData: [],
      backData: {},
      bucketData: {},
      //columns: [],
    };
  },
  watch: {
    visible(val) {
      this.visible = val;
    },
    code(val) {
      if (val.length > CODE_LENGTH) {
        console.log(val);
        this.newNum = "";
        let res = parseQRCode(val);
        /*         this.qrcode = JSON.parse(JSON.stringify(res));
        if (res) {
          this.testMaterial(res.palletNo, res.materialCode);
        } */
        this.getbucketDeatil(res.palletNo);
      } else {
        toast("此拍格式不对");
      }
      this.code = "";
      this.$refs.pmfocus.focus();
    },
  },
  methods: {
    // 消除获取焦点时自动弹出键盘
    stopKeyborad() {
      this.$refs.pmfocus.$refs.input.setAttribute("readonly", "readonly");
      setTimeout(() => {
        this.$refs.pmfocus.$refs.input.removeAttribute("readonly");
      }, 200);
    },
    //取消/确认键
    handleSure(action, done) {
      if (action === "confirm") {
        if (parseFloat(this.newNum) <= parseFloat(this.OldNum)) {
          console.log(this.newNum);
          this.findPat();
          done();
          this.$refs.pmfocus.focus();
        } else {
          toast("已超出桶量");
          this.newNum = "";
          done(false);
        }
      } else {
        done();
        this.$refs.pmfocus.focus();
      }
    },
    //获取当前单号的数据信息
    /*     async getReWorkList() {
      //this.$store.getters.productionLine
      let _this = this;
      let res = await getReWorkSearchList(this.$store.getters.productionLine);
      console.log(res);
      if (res && res.Status == 200) {
        let TempleData = JSON.parse(JSON.stringify(res.Data));
        TempleData.map((it) => {
          _this.columns.push(`${it.ProductId}-${it.ProductName}`);
        });
      } else {
        toast("请求连接失败，请联系管理员");
      }
    }, */
    //选择物料确认
    /*     SKUAdjustConfirm(val) {
      this.productorder = val.match(/[\w\W]+(?=-)/g)[0];
      console.log(this.productorder);
      this.SKUPicker = false;
      //this.index = index;
      this.rowData = [];
      this.getDetail(this.productorder);
      this.$refs.pmfocus.focus();
    }, */
    //获取SKU明细
    /*     async getDetail(material) {
      let res = await getSkuDeatil(
        this.$store.getters.productionLine,
        material
      );
      console.log(res);
      if (res.Status == 200 && res.Data) {
        this.backData = JSON.parse(JSON.stringify(res.Data));
        console.log(this.backData);
      } else {
        toast(res.Msg);
      } 
    }, */
    //检查桶是否被占用，获取桶的明细
    async getbucketDeatil(id) {
      if (id != "") {
        this.Code = id;
      }
      console.log(this.Code);
      let res = await getbucketDeatil(id);
      if (res.Status == 200 && res.Data) {
        if (!this.rowData.find((it) => it.BucketCode === this.Code)) {
          console.log(res);
          this.bucketData = res.Data;
          this.OldNum = res.Data.BucketWeight;
          this.newNum = res.Data.BucketWeight;
          this.visible = true;
        } else {
          toast("该桶已在列表中");
        }
      } else {
        if (res.Msg !== "桶编码不能为空") {
          toast(res.Msg);
        }
        console.log(res);
      }
    },
    //从外界添加数据
    async findPat() {
      let _this = this;
      /* if (
        !this.rowData.find((it) => it.ProductId === _this.backData.ProductId)
      ) { */
      console.log(this.Code);
      this.rowData.push({
        ProductName: _this.backData.ProductName,
        ProductId: _this.backData.ProductId,
        Num: _this.newNum,
        ProductionDate: _this.backData.WorkDate,
        BucketCode: _this.Code,
      });
      /*         let productionline = this.$store.getters.productionLine;
        let user = this.$store.getters.user;
        let res = await commissionSubmit(
          productionline,
          palletNo,
          materialCode,
          "7",
          user,
          "2",
          "",
          this.$store.getters.WarehouseId
        ); */
      /*  console.log(res);
        if (res && res.Status == 200 && res.Data) {
          this.rowData.push({
            ProductName: res.Data.ProductName,
            ProductId: res.Data.ProductId,
            UniqueId: res.Data.UniqueId,
            Num: res.Data.Num,
            ProductionDate: res.Data.ProductionDate.substring(0, 10),
            OutStorageBillDetailId: res.Data.OutStorageBillDetailId,
          }); 
        } else {
          toast(res.Msg);
        }*/
      /* } else {
        toast("此拍已被录入");
      } */
    },
    //提交
    async handleSubmit() {
      if (this.rowData.length > 0) {
        let items = [];
        console.log(this.rowData);
        this.rowData.map((it) => {
          items.push({
            BucketCode: it.BucketCode,
            Weight: it.Num,
          });
        });
        let res = await SubmitInStorage(
          this.$store.getters.user,
          this.$store.getters.productionLine,
          this.productorder,
          items
        );
        console.log(res);
        toast(res.Msg);
        this.rowData = [];
        items = [];
      } else {
        toast("暂无数据可提交");
      }
      this.$refs.pmfocus.focus();
    },
  },
  mounted() {
    //this.getReWorkList();
    this.$refs.pmfocus.focus();
  },
};
</script>
<style scoped>
.titlestyle {
  color: gray;
  margin-top: 0.1rem;
  margin-bottom: 0.5rem;
}
.checkbox {
  margin: 0.5rem 0 0.2rem 40%;
}
.ip-container {
  padding: 0.5rem;
}
</style>